/*
 * Copyright © 2009 Gerald Isaac Schwarz
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package base;

import java.awt.Point;
import java.util.ArrayList;
import condition.ConditionGroup;

public class Move {
	/**
	 * The value that signifies an endless repetition of this move.
	 */
	public static final int INFINITE = -1;
	
	/**
	 * The x,y distance between the piece and it's destination.
	 */
	private Point distance;
	
	/**
	 * The number of times the piece can repeat this move in a single turn.
	 */
	private int repetition;
	
	/**
	 * Whether or not the piece can perform this move when there isn't an enemy at the distance location.
	 */
	private boolean move;
	
	/**
	 * Whether or not the piece can perform this move when there is an enemy at the distance location.
	 */
	private boolean attack;
	
	private ArrayList<ConditionGroup<?>> conditionGroups;
	
	/**
	 * Initialize the move with all variable's specified.
	 * @param distance The x,y distance to the destination.
	 * @param repetition The number of times the move can be repeated in a single turn.
	 * @param move Whether or not the move can be performed when the destination is empty.
	 * @param attack Whether or not the move can be performed when the destination contains an enemy piece.
	 * @param conditionGroups
	 */
	public Move(Point distance, int repetition, boolean move, boolean attack, ArrayList<ConditionGroup<?>> conditionGroups) {
		this.distance = distance;
		this.repetition = repetition;
		this.move = move;
		this.attack = attack;
		this.conditionGroups = new ArrayList<ConditionGroup<?>>(conditionGroups);
	}
	
	/**
	 * Get the x,y distance between the piece and it's destination.
	 * @return The x,y distance between the piece and it's destination.
	 */
	public Point getDistance() {
		int x = this.distance.x;
		
		for (ConditionGroup<?> conditionGroup : this.conditionGroups) {
			if (conditionGroup.getName().equals("x") && conditionGroup.evaluate()) {
				if (conditionGroup.getType().equals("relative")) {
					x += (Integer) conditionGroup.getValue();
				}
				else if (conditionGroup.getType().equals("absolute")) {
					x = (Integer) conditionGroup.getValue();
				}
			}
		}
		
		int y = this.distance.y;
		
		for (ConditionGroup<?> conditionGroup : this.conditionGroups) {
			if (conditionGroup.getName().equals("y") && conditionGroup.evaluate()) {
				if (conditionGroup.getType().equals("relative")) {
					y += (Integer) conditionGroup.getValue();
				}
				else if (conditionGroup.getType().equals("absolute")) {
					y = (Integer) conditionGroup.getValue();
				}
			}
		}
		
		return new Point(x,y);
	}
	
	/**
	 * Get the number of times the move can be repeated in a single turn.
	 * @return The number of times the move can be repeated. 0 if it can only be performed once. Move.INFINITE if it can be repeated infinitely.
	 */
	public int getRepetition() {
		int repetition = this.repetition;
		
		for (ConditionGroup<?> conditionGroup : this.conditionGroups) {
			if (conditionGroup.getName().equals("repetition") && conditionGroup.evaluate()) {
				if (conditionGroup.getType().equals("relative")) {
					repetition += (Integer) conditionGroup.getValue();
				}
				else if (conditionGroup.getType().equals("absolute")) {
					repetition = (Integer) conditionGroup.getValue();
				}
			}
		}
		
		return repetition;
	}
	
	/**
	 * Check Whether or not the move can be performed when the destination is empty.
	 * @return True if it can, false if it can't.
	 */
	public boolean isMove() {
		boolean move = this.move;
		
		for (ConditionGroup<?> conditionGroup : this.conditionGroups) {
			if (conditionGroup.getName().equals("move") && conditionGroup.evaluate()) {
				if (conditionGroup.getType().equals("relative")) {
					move = !move;
				}
				else if (conditionGroup.getType().equals("absolute")) {
					move = (Boolean) conditionGroup.getValue();
				}
			}
		}
		
		return move;
	}
	
	/**
	 * Check whether or not the move can be performed when the destination contains an enemy piece.
	 * @return True if it can, false if it can't.
	 */
	public boolean isAttack() {
		boolean attack = this.attack;
		
		for (ConditionGroup<?> conditionGroup : this.conditionGroups) {
			if (conditionGroup.getName().equals("attack") && conditionGroup.evaluate()) {
				if (conditionGroup.getType().equals("relative")) {
					attack = !attack;
				}
				else if (conditionGroup.getType().equals("absolute")) {
					attack = (Boolean) conditionGroup.getValue();
				}
			}
		}
		
		return attack;
	}
	
	public ArrayList<ConditionGroup<?>> getConditionGroups() {
		return new ArrayList<ConditionGroup<?>>(this.conditionGroups);
	}
}
